#!/bin/bash
# init script for the openQRM lcmc plugin
#
# openQRM Enterprise developed by openQRM Enterprise GmbH.
#
# All source code and content (c) Copyright 2014, openQRM Enterprise GmbH unless specifically noted otherwise.
#
# This source code is released under the GNU General Public License version 2, unless otherwise agreed with openQRM Enterprise GmbH.
# The latest version of this license can be found here: src/doc/LICENSE.txt
#
# By using this software, you acknowledge having read this license and agree to be bound thereby.
#
#           http://openqrm-enterprise.com
#
# Copyright 2014, openQRM Enterprise GmbH <info@openqrm-enterprise.com>
#
OPENQRM_SERVER_BASE_DIR=$(pushd $(dirname $0)/../../../../.. > /dev/null; echo $PWD; popd > /dev/null)
. $OPENQRM_SERVER_BASE_DIR/openqrm/etc/openqrm-server.conf
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-server-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/lcmc/include/openqrm-plugin-lcmc-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/lcmc/etc/openqrm-plugin-lcmc.conf

LCMC_MC_VERSION="1.3.2"

function openqrm_plugin_lcmc_start() {
	echo "Starting the openQRM lcmc-plugin"
	openqrm_plugin_lcmc_stop 1>/dev/null 2>&1
	touch $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/lcmc/.running
	return $RET
}


function openqrm_plugin_lcmc_stop() {
	echo "Stopping the openQRM lcmc-plugin"
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/lcmc/.running
	return 0
}


function openqrm_plugin_lcmc_init() {
	echo "Initializing the openQRM lcmc-plugin"
	# copy lcmc-applet to the cloud
	mkdir -p $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/cloud/cloud-portal/web/user/lcmc/
	cd $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/cloud/cloud-portal/web/user/lcmc/
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/lcmc/web/lcmc-gui.php .
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/lcmc/web/LCMC-applet-"$LCMC_MC_VERSION".jar .
	cd - 1>/dev/null
	cp -f $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/lcmc/web/img/plugin.png $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/cloud/cloud-portal/web/img/ha_console.png
	# linking the web dir
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/lcmc/web $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/lcmc
	return 0
}


function openqrm_plugin_lcmc_uninstall() {
	echo "Uninstalling the openQRM lcmc-plugin"
	openqrm_plugin_lcmc_stop
	# remove from cloud
	rm -rf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/cloud/cloud-portal/web/user/lcmc
	rm -f $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/cloud/cloud-portal/web/img/ha_console.png
	# unlinking the web dir
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/lcmc
}



case "$1" in
	start)
		openqrm_plugin_lcmc_start
		;;
	stop)
		openqrm_plugin_lcmc_stop
		;;
	restart)
		openqrm_plugin_lcmc_stop
		sleep 1
		openqrm_plugin_lcmc_start
		;;
	init)
		openqrm_plugin_lcmc_init
		;;
	uninstall)
		openqrm_plugin_lcmc_uninstall
		;;
	*)
		echo "Usage: $0 {start|stop|restart|init|uninstall}"
		exit 1

esac
exit $?


